package com.ssyc.gsk_teacher.activity;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.view.NoScrollViewPager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.fragment.TeacherClassAnalyFragment;
import com.ssyc.gsk_teacher.fragment.TeacherStudentAnalyFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 分析界面
 * Created by Administrator on 2018/7/1 0001.
 */

public class TeacherStudAnalyzeActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private ImageView ivBack;

    //学生
    private ImageView lvStudent;

    //班级
    private ImageView lvClass;

    //不可滚动ViewPager
    private NoScrollViewPager vp;

    //填充ViewPager的页面
    private List<Fragment> mFragments;

    //ViewPager的适配器
    private VpCommonAdapter vpAdapter;

    //班级号
    private String classNum;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.teacher_student_select, R.drawable.teacher_class_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.teacher_student_normal, R.drawable.teacher_class_analy_normal};


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_student_analyze;
    }

    @Override
    protected void init() {

        showContent();

        //获取班级号
        initInent();

        //初始化视图
        initView();

        //设置适配器
        initVp();
    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        lvStudent = findViewById(R.id.lv_student);
        lvStudent.setOnClickListener(this);
        lvClass = findViewById(R.id.iv_class);
        lvClass.setOnClickListener(this);
        vp = findViewById(R.id.vp);
        vp.setPagerEnabled(false);
    }

    /**
     * 获取班级号
     */
    private void initInent() {
        classNum = getIntent().getStringExtra("classnum");
    }

    /**
     * 初始化VieWPager
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(TeacherStudentAnalyFragment.newInstance(classNum));
        mFragments.add(TeacherClassAnalyFragment.newInstance(classNum));
        vpAdapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setAdapter(vpAdapter);
    }


    /**
     * 同步ViewPager页面和切换选中Tab的方法
     *
     * @param pos
     */
    private void syncVpState(int pos) {
        vp.setCurrentItem(pos);
        if (pos == 0) {
            lvStudent.setImageResource(tabSelectIds[0]);
            lvClass.setImageResource(tabUnselectIds[1]);
        } else if (pos == 1) {
            lvStudent.setImageResource(tabUnselectIds[0]);
            lvClass.setImageResource(tabSelectIds[1]);
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.lv_student) {
            //学生
            syncVpState(0);
        } else if (id == R.id.iv_class) {
            //班级
            syncVpState(1);
        }
    }
}
